<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const arr = [
        { id: "29", pid: "", name: "总裁办", children: [] },
        {
          id: "2c",
          pid: "",
          name: "财务部",
          children: [
            { id: "2d", pid: "2c", name: "财务核算部", children: [] },
            { id: "2f", pid: "2c", name: "薪资管理部", children: [] },
          ],
        },
        {
          id: "d2",
          pid: "",
          name: "技术部",
          children: [
            {
              id: "d3",
              pid: "d2",
              name: "Java研发部",
              children: [
                { id: "d31", pid: "d3", name: "Java研发部", children: [] },
              ],
            },
          ],
        },
      ];

      // 树形结构转平铺
      const treeToList = (tree) => {
        let res = [];
        function flat(arr) {
          if (arr && arr.length > 0) {
            arr.forEach((item) => {
              res.push({ id: item.id, pid: item.pid, name: item.name });
              flat(item.children);
            });
          }
        }

        flat(tree);
        return res;
      };

      const list = treeToList(arr);
    </script>
  </body>
</html>
